#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2005;
const int M = 1e6 + 5;
int n, m;
int a[N];
int head[N], deg[N], f[N], d[N];
int ver[M], Next[M], tot;
void add(int u, int v) {
  ++tot;
  ver[tot] = v;
  Next[tot] = head[u];
  head[u] = tot;
}
void toposort() {
  queue<int> q;
  rep(i, 1, n + m) if (!deg[i]) q.push(i);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = Next[i]) {
      int v = ver[i];
      if (--deg[v] == 0) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
  set<int> st;
  rep(i, 1, n) st.insert(d[i]);
  cout << st.size();
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, m) {
    int k;
    cin >> k;
    rep(j, 1, k) cin >> a[j];
    int id = a[1];
    rep(j, 1, k) {
      while (id < a[j]) add(n + i, id), deg[id]++, ++id;
      add(id, n + i), deg[n + i]++;
      id++;
    }
  }
  toposort();
  return 0;
}